<template>
  <MessageBubble :isMine="false">
    <div>
      {{ text }}
      <el-button v-if="isJoinGroupRequest" type="text" @click="showDialog = true">处理</el-button>
      <el-dialog title="处理加群申请" :visible.sync="showDialog" width="30%">
        <el-form ref="form" v-model="form" label-width="100px">
          <el-form-item label="处理结果：">
            <el-radio-group v-model="form.handleAction">
              <el-radio label="Agree">同意</el-radio>
              <el-radio label="Reject">拒绝</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="附言：">
            <el-input
              type="textarea"
              resize="none"
              :rows="3"
              placeholder="请输入附言"
              v-model="form.handleMessage"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="handleGroupApplication">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </MessageBubble>
</template>

<script>
import MessageBubble from '../MessageBubble';

export default {
  name: 'GroupSystemNoticeElement',
  components: {
    MessageBubble
  }, 
  props: {
    message: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      showDialog: false,
      form: {
        handleAction: 'Agree',
        handleMessage: ''
      }
    }
  },
  computed:{
    text(){
      const groupName = this.message.payload.groupProfile.groupName || this.message.payload.groupProfile.groupID;
      switch (this.message.payload.operationType) {
        case 1:
          return `${this.message.payload.operatorID} 申请加入群组：${groupName}`;
        case 2:
          return `成功加入群组：${groupName}`;
        case 3:
          return `申请加入群组：${groupName}被拒绝`;
        case 4:
          return `被管理员${this.message.payload.operatorID}踢出群组：${groupName}`;
        case 5:
          return `群：${groupName} 已被${this.message.payload.operatorID}解散`;
        case 6:
          return `${this.message.payload.operatorID}创建群：${groupName}`;
        case 7:
          return `${this.message.payload.operatorID}邀请你加群：${groupName}`;
        case 8:
          return `你退出群组：${groupName}`;
        case 9:
          return `你被${this.message.payload.operatorID}设置为群：${groupName}的管理员`;
        case 10:
          return `你被${this.message.payload.operatorID}撤销群：${groupName}的管理员身份`;
        case 255:
          return '自定义群系统通知: ' + this.message.payload.userDefinedField;
      }
      return 'null';
    },
    isJoinGroupRequest() {
      return this.message.payload.operationType === 1;
    }
  },
  methods: {
    handleGroupApplication() {
      this.tim.handleGroupApplication({
          handleAction: this.form.handleAction,
          handleMessage: this.form.handleMessage,
          message: this.message
        }).then(() => {
          this.showDialog = false;
          this.$store.commit('removeMessage', this.message);
        }).catch(error => {
          this.$store.commit('showMessage', { type: 'error', message: error.message });
          this.showDialog = false;
        });
    }
  }
}
</script>